package com.aaa.order.controller;

import com.aaa.order.entity.TbOrdInf;
import com.aaa.order.service.impl.TbOrdInfServiceImpl;
//import com.aaa.order.util.ConnUtil;
import com.rabbitmq.client.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("order")
public class TbOrdInfController {

    @Resource
    TbOrdInfServiceImpl tbOrdInfServiceImpl;

    @RequestMapping("selectAll")
    public List<TbOrdInf> selectAll(){
        return tbOrdInfServiceImpl.listDesc();
    }

    /*根据用户查全部订单*/
    @RequestMapping("findByTbid")
    public List<TbOrdInf> findByTbid(String accountId,Integer status,String lableName){
        return tbOrdInfServiceImpl.findByTbid(accountId,status,lableName);
    }

    @RequestMapping("saveOrUpdate")
    public boolean saveOrUpdate(TbOrdInf tbOrdInf){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tbOrdInf.setUpdateDate(dateString);

        tbOrdInf.setCreateBy("");

        boolean b = tbOrdInfServiceImpl.saveOrUpdate(tbOrdInf);

        return b;
    }

    @RequestMapping("saveOrUpdate3")
    public boolean saveOrUpdate3(String oid,Integer status,Integer statu){
        if (statu == 0){
            TbOrdInf tbOrdInf = new TbOrdInf();
            tbOrdInf.setOid(oid);
            tbOrdInf.setStatus(status);
            boolean b = tbOrdInfServiceImpl.updateById(tbOrdInf);
            return b;
        }else {
            return false;
        }
    }

    @RequestMapping("saveOrUpdate1")
    public String saveOrUpdate1(TbOrdInf tbOrdInf) throws IOException, TimeoutException, ParseException {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tbOrdInf.setUpdateDate(dateString);

        //设定15分钟
        long time = 15*60*1000;
        //createDate+15分钟
        //afterDate订单过期时间
        Date afterDate = new Date(date.getTime() + time);
        String createBy = formatter.format(afterDate);
        tbOrdInf.setCreateBy(createBy);

        boolean b = tbOrdInfServiceImpl.saveOrUpdate(tbOrdInf);
        String oid = "";
        if (b){
            oid = tbOrdInf.getOid();
        }
        return oid;
    }


    @RequestMapping("pay")
    public boolean pay(String oid) throws IOException, TimeoutException {
//        Connection conn = ConnUtil.getConn();
//
//        //创建信道
//        Channel channel = conn.createChannel();
//
//        //队列名
//        String queueName = "my-que";
//        channel.queueDeclare(queueName,false,false,false,null);
//
//        //消费者
//        DefaultConsumer consumer = new DefaultConsumer(channel){
//            /**
//             * 应答的处理器:当消费者获取从队列中获取到消息时的回调函数,进行消费
//             * @param consumerTag 消费者表示
//             * @param envelope
//             * @param properties 参数信息
//             * @param body 消息内容
//             * @throws IOException
//             */
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                System.out.println("前台oid"+oid);
//                System.out.println("consumerTag:"+consumerTag);
//                // 应答标识
//                System.out.println("deliveryTag:"+envelope.getDeliveryTag());
//                System.out.println("envelope:"+envelope.getExchange());
//                System.out.println("获取到的消息内容:"+new String(body));
//            }
//        };
//
//        // 消费者监听队列
//        // basicConsume(队列名,是否立即应答,消费者)
//        //5、监听队列
//        // true:表示自动确认，只要消息从队列中获取，无论消费者获取到消息后是否成功消费，都会认为 消息已经成功消费
//        // false:表示手动确认，消费者获取消息后，服务器会将该消息标记为不可用状态，等待消费者的 反馈， 如果消费者一直没有反馈，那么该消息将一直处于不可用状态，并且服务器会认为该消 费者已经挂掉，不会再给其 发送消息，直到该消费者反馈。
//        // */
//        channel.basicConsume(queueName,true,consumer);
//        channel.close();;
//        conn.close();
        return true;
    }
    @RequestMapping("delete")
    public boolean delete(Integer id){
        return tbOrdInfServiceImpl.removeById(id);
    }

    @RequestMapping("findById/{tid}")
    public TbOrdInf findById(@PathVariable String tid){
        return tbOrdInfServiceImpl.findById(tid);
    }

    @RequestMapping("findByOid")
    public TbOrdInf findByOid(String oid) throws ParseException {
        TbOrdInf byOid = tbOrdInfServiceImpl.findByOid(oid);
        return byOid;
    }

    //模糊查询
    @RequestMapping("listBySname")
    public List<TbOrdInf> listByLname(String shippingName){
        return tbOrdInfServiceImpl.listBySname(shippingName);
    }


    /**
     * 活动商品添加进mq
     * @param tbOrdInf
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    @RequestMapping("saveOrUpdate2")
    public String saveOrUpdate2(TbOrdInf tbOrdInf) throws IOException, TimeoutException {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tbOrdInf.setUpdateDate(dateString);

        boolean b = tbOrdInfServiceImpl.saveOrUpdate(tbOrdInf);
        String oid = "";
        if (b){
            oid = tbOrdInf.getOid();
        }
        return oid;
    }




}
